package com.zzit.vjv.controller.documentReview_controller.developer;

import com.zzit.vjv.biz.documentReview_biz.developer.DevUpdBiz;
import com.zzit.vjv.entity.documentReview_entity.AppInfo;
import com.zzit.vjv.entity.documentReview_entity.AppVersion;
import com.zzit.vjv.entity.documentReview_entity.DevUser;
import com.zzit.vjv.util.FileUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller("devUpdController")
@RequestMapping("upd")
public class DevUpdController {


    @Resource(name = "devUpdBiz")
    private DevUpdBiz devUpdBiz;


    /**
     * 修改app信息方法
     *
     * @param appInfo
     * @param attach
     * @param request
     * @return
     */
    @RequestMapping("/updAppInfo")
    public String updAppInfo(@ModelAttribute AppInfo appInfo,
                             @RequestParam MultipartFile attach,
                             HttpServletRequest request,
                             HttpSession session) throws IOException {


        System.out.println("对象信息" + appInfo);

        //调用文件上传帮助类,将绝对以及相对路径接收(用于修改logo)
        Map<String, Object> fileMap = FileUtil.numerousFile(attach, request, "statics");

        System.err.println("相对" + fileMap.get("xd"));
        System.err.println("绝对" + fileMap.get("jd"));

        //用于传输更新者ID
        DevUser devUser = (DevUser) session.getAttribute("devUserSession");

        //用于向修改sql进行传值
        Map<String, Object> map = new HashMap<>();
        map.put("id", appInfo.getId());
        map.put("softwareName", appInfo.getSoftwareName());
        map.put("supportROM", appInfo.getSupportROM());
        map.put("interfaceLanguage", appInfo.getInterfaceLanguage());
        map.put("softwareSize", appInfo.getSoftwareSize());
        map.put("downloads", appInfo.getDownloads());
        map.put("flatformId", appInfo.getFlatformId());
        map.put("categoryLevel1", appInfo.getCategoryLevel1());
        map.put("categoryLevel2", appInfo.getCategoryLevel2());
        map.put("categoryLevel3", appInfo.getCategoryLevel3());
        map.put("appInfo", appInfo.getAppInfo());
        map.put("logoPicPath", fileMap.get("xd").toString());
        map.put("logoLocPath", fileMap.get("jd").toString());
        map.put("modifyBy", devUser.getId());
        map.put("status", appInfo.getStatus());


        if (appInfo.getStatus() > 0) {

            //调用修改app信息方法,并且重新提交审核
            devUpdBiz.updAppInfoAndStatusById(map.size() > 0 ? map : null);

        } else {

            //调用修改app信息方法
            int row = devUpdBiz.updAppInfoById(map.size() > 0 ? map : null);
            System.out.println("成功?" + row);
        }


        System.err.println("状态" + appInfo.getStatus());


        return "redirect:/dev/devList";


    }


    /**
     * 前台修改最新aap版本方法
     * @param appVersion
     * @param attach
     * @param request
     * @param session
     * @return
     * @throws IOException
     */
    @RequestMapping("/updAppVersion")
    public String updAppVersion(@ModelAttribute AppVersion appVersion
                              ,@RequestParam MultipartFile attach
                               ,HttpServletRequest request,HttpSession session) throws IOException {


        Map<String,Object> map=new HashMap<>();

        Map<String,Object> fileMap=FileUtil.numerousFile(attach,request,"statics");

        DevUser devUser= (DevUser) session.getAttribute("devUserSession");



        map.put("id",appVersion.getId());
        map.put("versionSize",appVersion.getVersionSize());
        map.put("versionInfo",appVersion.getVersionInfo());
        map.put("publishStatus",appVersion.getPublishStatus());
        map.put("downloadLink",fileMap.get("xd")==null?appVersion.getDownloadLink():fileMap.get("xd").toString());
        map.put("apkLocPath",fileMap.get("jd")==null?appVersion.getApkLocPath():fileMap.get("jd").toString());
        map.put("modifyBy",devUser.getId());


        int row=devUpdBiz.updAppVersionById(map.size()>0?map:null);


        System.out.println("对象信息"+appVersion);

        System.out.println("成功?"+row);



        return "redirect:/dev/devList";

    }


    /**
     * 用于前台app上架或下架方法
     * @param appId
     * @param status
     * @return
     */
    @RequestMapping("/updAppInfoStatus")
    @ResponseBody
    public Map<String,Object> updAppInfoStatus(Integer appId,Integer status){


        Map<String,Object> map=new HashMap<>();

        if(status==4){

            int row=devUpdBiz.updAppInfoStatusById(appId, status);

            map.put("errorCode","0");
            map.put("resultMsg","success");
            if(row==0){

                map.put("errorCode","1");
                map.put("errorCode1","exception000001");

            }


        }else{


            int row=devUpdBiz.updAppInfoStatusById(appId, status);

            map.put("errorCode","0");
            map.put("resultMsg","success");
            if(row==0){

                map.put("errorCode","1");
                map.put("errorCode1","exception000001");

            }





        }


        System.err.println("aid"+appId+"状态"+status);





        return map;
    }


}
